package com.im.web.response;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应类
 * @param <T> 数据类型
 */

@Data
public class Result<T> implements Serializable {
    private int code;
    private String message;
    private T data;
    private long timestamp;

    public Result() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ReturnCode.SUCCESS.getCode());
        result.setMessage(ReturnCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> fail(ReturnCode returnCode) {
        Result<T> result = new Result<>();
        result.setCode(returnCode.getCode());
        result.setMessage(returnCode.getMessage());
        return result;
    }

    public static <T> Result<T> fail(ReturnCode returnCode, String message) {
        Result<T> result = new Result<>();
        result.setCode(returnCode.getCode());
        result.setMessage(message);
        return result;
    }

}